using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Net.Mail;
using Pervote.Core;
using Pervote.Core.Logic;

public partial class PollCreated : System.Web.UI.Page
{
    string pollId;

    protected void Page_Load(object sender, EventArgs e)
    {
        pollId = Request["pollId"];
        ViewState["pollId"] = pollId;
    }

    protected string GetPollLink()
    {
        return pollId.ToString();
    }

    protected void inviteButton_Click(object sender, EventArgs e)
    {
        string pollLink = "/PollMain.aspx?id=" + ViewState["pollId"];            
        try
        {
            Poll poll = new PollManager().GetPollView(long.Parse(pollId), null, new ErrorInfo());
            string inviter = userNameTextBox.Text;
            if (inviter == "") inviter = "A friend";
            string[] _mailIds = friendListTextBox.Text.Split(',', ';');
            List<string> mailIds = new List<string>();
            foreach (string mailId in _mailIds)
            {
                mailIds.Add(mailId.Trim());
            }
            MailAddress bccAddress;
            if (mailIds.Count <= 20)
            {
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Host = "mail.pervote.com";
                smtpClient.Port = 25;
                MailMessage message = new MailMessage();
                message.Subject = inviter + " has invited you to vote at pervote.com";
                message.Body = "<html><body>" +
                    "<p>" + inviter + " has created a poll at pervote.com, named " + poll.Title + "." +
                    "Please go to <a href=\"http://www.pervote.com" + pollLink + "\"> Pervote.com </a> to vote.</p>" +
                    "</body></html>";
                message.To.Add("users@pervote.com");
                message.IsBodyHtml = true;
                message.From = new MailAddress("pollmaster@pervote.com");
                foreach (string mailId in mailIds)
                {
                    bccAddress = new MailAddress(mailId);
                    message.Bcc.Add(bccAddress);
                }
                smtpClient.Send(message);
            }
        }
        catch
        {
            //
        }
        //send...
        Response.Redirect(pollLink);
    }
}
